package com.zlw.test.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zlw.test.config.MyInfo;
import com.zlw.test.model.Goods;
import com.zlw.test.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;

/**
 * @ClassName GoodsController
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2021/1/22  19:45
 * @Version 1.0
 */
@Slf4j
@ApiIgnore
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Value("${my.info.desc}")
    private String desc;

    @Autowired
    private MyInfo myInfo;

    @Autowired
    private Environment environment;


    @GetMapping(value = "/byId")
    public JSONObject goods(Integer id) {
        log.info("[根据id获取商品信息]");

        Goods goods = goodsService.selectByPrimaryKey(id);
        log.info("[返回值] {} ", goods);
        return JSONObject.parseObject(JSONObject.toJSONString(goods));
    }

    /**
     * @param
     * @return java.lang.Object
     * @description TODO
     * @author zhaoluowei
     * @date 2021/1/28 20:38
     */
    @RequestMapping("/web/test")
    @ResponseBody
    public Object test() {
        int a = 10 / 0; //抛出异常，显示一个背景白色的错误页面，不是很友好
        return "cors " + desc + myInfo.getPhone() + environment.getProperty("my.info.address");
    }


    @RequestMapping("/web/test2")
    @ResponseBody
    public Object test2() throws FileNotFoundException {
//        int a = 10 / 0; //抛出异常，显示一个背景白色的错误页面，不是很友好

        FileOutputStream fileOutputStream = new FileOutputStream("c:/dd/.txt");


        return "cors " + desc + myInfo.getPhone() + environment.getProperty("my.info.address");
    }


    @RequestMapping("/web/test3")
    @ResponseBody
    public JSONObject test3() {
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("body","cors " + desc + myInfo.getPhone() + environment.getProperty("my.info.address"));
        return JSONObject.parseObject(JSONObject.toJSONString(hashMap));
    }


}
